﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Text;
using System.Net.Mail;

public partial class admin_members_add_member : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ( Roles.IsUserInRole("Role_Uni_Admin") == true){
            int uni_id = ACSService.getAdminUni(Membership.GetUser().ProviderUserKey.ToString());
            //UniID.Value = uni_id
            uniList.SelectedValue = uni_id.ToString();
            uniList.Enabled = false;
        }else if (Roles.IsUserInRole("Role_Super_Admin") == true){
            
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Boolean ctzn = null;
        //if(Boolean.TryParse(citizenList.SelectedValue.ToString() , true) == true)
        //    ctzn = Boolean.Parse(citizenList.SelectedValue.ToString());
        //}

        string fname = fnameBox.Text;
        string lname = lnameBox.Text;
        string email = emailBox.Text;
        string phone = phoneBox.Text;
        string studentid = studentBox.Text;
        int uniID = Convert.ToInt32(uniList.SelectedValue);
        string subject = SubjectList.SelectedItem.Text;
        string year = yearList.SelectedItem.Text;
        string career = careerList.SelectedItem.Text;
        string degree = DegreeList.SelectedItem.Text;
        string gender = genderList.SelectedValue.ToString();
        string ethnicity = ethnicityList.SelectedItem.Text;
        Boolean ukcitizen = false; //Convert.ToBoolean(citizenList.SelectedValue);
        string age = ageList.SelectedValue.ToString();
        Boolean paid = paidBox.Checked;

        Boolean exist = ACSService.IsMemberExist(email, uniID);
        if (exist)
        {
            msgLabel.Text = "This member already exist";
        }
        else
        {
            Boolean insert = ACSService.addNewMember(fname, lname, email, phone, studentid, uniID, subject, year, degree, career, gender, ethnicity, ukcitizen, age, paid);

            if (insert)
            {
                StringBuilder str = new StringBuilder();
                str.AppendLine("<p>Hello " + fname + " " + lname + ", your registration with ACS was successful</p>");
                str.AppendLine("<p>Congratulations, your registration has been successful. Your ACS will be in contact soon about the next steps.</p>");
                str.AppendLine("<p>In the meantime, why not share the news with your friends.</p>");
                str.AppendLine("<p>Copy and paste this simple message on facebook, twitter and other social networks. \"I just joined my university's African Caribbean Society. You can sign-up through myacs.co.uk\"</p>");
                str.AppendLine("<p>Have fun,</p>");
                str.AppendLine("<p>ACS Project</p>");

                string body = str.ToString();

                MailMessage message = new MailMessage(new MailAddress("no-reply@myacs.co.uk", "ACS Membership"), new MailAddress(email));

                message.Subject = "Welcome to ACS";
                message.Body = body;
                message.BodyEncoding = Encoding.ASCII;
                message.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient();
                try{
                    smtp.Send(message);
                }catch(Exception ex){
                    //mail not sent
                }
                msgLabel.Text = "New member added";
            }
            else
            {
                msgLabel.Text = "Error: member not added";
            }
        }
    }
}